package com.mx.contror;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mx.entity.Person;
import com.mx.service.PersonService;

@Controller
public class PersonController {
	//注入我们的service
	@Resource(name="PersonService")
	private PersonService  personserver;
	
 @RequestMapping("/add")
 @ResponseBody
	public  List<Person>  add(HttpSession session,HttpServletRequest  request,HttpServletResponse  response) throws IOException{
	 String name=request.getParameter("name");
	 System.out.println(name);
	 List<Person> list= personserver.showAll();
	 //session.setAttribute("list", list);
	 System.out.println("list"+list);
	 return list;
	}
 
 
 //
 @RequestMapping("/testb")
 public  String testb(){
	  System.out.println("b");
	 return "b";
 }
 /**
  * @author Mx
  *  实现伪静态 (路径参数)
  * //使用@PathVariable 获取我们地址栏的值   第一个参数 是我们url  name  第二个是 申明字符串类型  第三个是  我们接收到存到的变量中
  **/
 @RequestMapping("/showwjt/test/{name}/{age}")
 public String showWjt(ModelMap  map,@PathVariable("name") String name,@PathVariable("age") int age){  	
	 //输出name
	 System.out.println(name);
	 System.out.println(age);
	 return "wht";
	 
 }
 /**
  * 获取  URL参数
  * 我们的url= http://localhost：8080/SpringMVCTest/showUrl/url?name=zhangsan&age=20
  * @PathVariable获取url的参数值   第一个志明类型   第二个我们接收存到的变量中
  * */
  @RequestMapping(value="/showUrl/url",method=RequestMethod.GET)
  public String getUrl(ModelMap  map,@RequestParam String name,@RequestParam int age){
	   //测试结果
	  System.out.println("名字"+name);
	  return null;
  }
   //addperson
  @RequestMapping(value="/addperson",method=RequestMethod.GET)
  public String addperson(ModelMap  map,@RequestParam String name,@RequestParam int age){
	   Person  person=new Person();
	   person.setName(name);
	   person.setPwd(name);
	   person.setDesc("1111");
	   personserver.addperson(person);
	  return "showperson";
  }
  //依据ID删除用户
  @RequestMapping(value="/delpersonById")
  public String delPerson(){
	  personserver.delPerson("2");
	  return null;
  }
  //查询所有
  @RequestMapping("/getAllUesr")
  @ResponseBody
  public List<Person> getAllUesr(){
	 List<Person> mlist=personserver.showAll();
	return mlist;
	  
  }
//查询所有 angularjs
  @RequestMapping("/getAllUesrJs/{type}")
  @ResponseBody
  public List<Person> getAllUesrByjs(@PathVariable("type") String type){
	 Person  person=new Person("aaa", "aaa", "aaaaa4");
	 Person  person1=new Person("aaa1", "aaa3", "aaaaa4");
	 Person  person2=new Person("aaa1", "aaa3", "aaaaa4");
	 Person  person3=new Person("aaa1", "aaa3", "aaaaa4");
	 List<Person> list=new ArrayList<Person>();
	 list.add(person);
	 list.add(person1);
	 list.add(person2);
	 list.add(person3);
	return list;
	  
  }
}
